package com.wwzh.interview.study.thread;

import java.util.concurrent.*;

public class MyThreadPoolDemo {
    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime().availableProcessors());
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                1L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());

        try {
            // 模拟10个用户来办理业务，每个用户就是一个来自外部的请求处理线程
            for (int i= 1;i<=10;i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 办理业务");
                });

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }

    private static void threadPoolInit() {
        //        ExecutorService threadPool  = Executors.newFixedThreadPool(5);  // 一池5各处理线程
//        ExecutorService threadPool  = Executors.newSingleThreadExecutor();  // 一池1各处理线程
        ExecutorService threadPool  = Executors.newCachedThreadPool();  // 一池N各处理线程
        try {
            // 模拟10个用户来办理业务，每个用户就是一个来自外部的请求处理线程
            for (int i= 1;i<=10;i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 办理业务");
                });

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }
}
